<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>问卷管理</el-breadcrumb-item>
      <el-breadcrumb-item>问卷列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片主体区 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">添加空白问卷</el-button>
        </el-col>
      </el-row>

      <!-- 问卷列表区域 -->
      <el-table :data="questionnaireList" stripe>
        <el-table-column prop="questionnaireTitle" label="问卷标题">
        </el-table-column>
        <!-- <el-table-column prop="startTime" label="开始时间">
        </el-table-column>
        <el-table-column prop="endTime" label="结束时间">
        </el-table-column> -->
        <el-table-column prop="accessUrl" label="访问链接">
        </el-table-column>
        <el-table-column prop="questionnaireState" label="问卷状态">
        </el-table-column>
        <el-table-column prop="creatorAccount" label="创建人">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="添加问题" placement="top" :enterable="false">
              <!-- 添加问题按钮 -->
              <el-button type="primary" icon="el-icon-plus" @click="showAddQuestionDialog(scope.row.id)" size="small"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="填写问卷" placement="top" :enterable="false">
              <!-- 填写按钮 -->
              <el-button type="success" icon="el-icon-tickets" @click="doQuestionnaire(scope.row.id)" size="small"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="统计答案" placement="top" :enterable="false">
              <!-- 统计按钮 -->
              <el-button type="warning" icon="el-icon-s-marketing" @click="statisticQuestionnaire(scope.row.id)" size="small"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除问卷" placement="top" :enterable="false">
              <!-- 删除按钮 -->
              <el-button type="danger" icon="el-icon-delete" @click="deleteQuestionnaire(scope.row.id)" size="small"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="querryInfo.current" :page-sizes="[1, 2, 5, 10]" :page-size="querryInfo.size" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
    <!-- 添加空白问卷的对话框 -->
    <el-dialog title="添加空白问卷" :visible.sync="addDialogVisible" width="50%" @close="addDialogClosed">
      <!-- 内容主题区域 -->
      <div>
        <el-form ref="addQuestionnaireRef" :model="addQuestionnaireForm" label-width="70px" :rules="addQuestionnaireRules">
          <el-form-item label="问卷标题" prop="questionnaireTitle">
            <el-input v-model="addQuestionnaireForm.questionnaireTitle"></el-input>
          </el-form-item>
          <el-form-item label="问卷说明" prop="questionnaireExplain">
            <el-input v-model="addQuestionnaireForm.questionnaireExplain"></el-input>
          </el-form-item>
          <!-- <el-form-item label="开始时间" prop="startTime">
            <el-input v-model="addQuestionnaireForm.startTime"></el-input>
          </el-form-item>
          <el-form-item label="结束时间" prop="endTime">
            <el-input v-model="addQuestionnaireForm.endTime"></el-input>
          </el-form-item> -->
          <el-form-item label="创建人" prop="creatorAccount">
            <el-input v-model="addQuestionnaireForm.creatorAccount"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addQuestionnaire">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加问题的对话框 -->
    <el-dialog title="添加问题" :visible.sync="addQuestionVisible" width="50%" @close="addQuestionDialogClosed">
      <!-- 内容主题区域 -->
      <div>
        <el-form ref="addQuestionRef" :model="addQuestionForm" label-width="70px" :rules="addQuestionRules">
          <el-form-item label="问题内容" prop="questionnaireTitle">
            <el-input v-model="addQuestionForm.questionTitle"></el-input>
          </el-form-item>
          <el-form-item label="问题类型" prop="questionnaireTitle">
            <el-radio v-model="addQuestionForm.type" label="1">单选</el-radio>
            <el-radio v-model="addQuestionForm.type" label="2">多选</el-radio>
            <el-radio v-model="addQuestionForm.type" label="3">单行填空</el-radio>
            <el-radio v-model="addQuestionForm.type" label="4">多行问答</el-radio>
          </el-form-item>
          <!-- 选项内容 -->
          <el-form-item v-if="addQuestionForm.type === '1' || addQuestionForm.type === '2'" label="选项1">
            <el-input v-model="addQuestionForm.options[0].content" placeholder="请输入内容"></el-input>
          </el-form-item>
          <el-form-item v-if="addQuestionForm.type === '1' || addQuestionForm.type === '2'" label="选项2">
            <el-input v-model="addQuestionForm.options[1].content" placeholder="请输入内容"></el-input>
          </el-form-item>
          <el-form-item v-if="addQuestionForm.type === '1' || addQuestionForm.type === '2'" label="选项3">
            <el-input v-model="addQuestionForm.options[2].content" placeholder="请输入内容"></el-input>
          </el-form-item>
          <el-form-item v-if="addQuestionForm.type === '1' || addQuestionForm.type === '2'" label="选项4">
            <el-input v-model="addQuestionForm.options[3].content" placeholder="请输入内容"></el-input>
          </el-form-item>
          <!-- 是否必填 -->
          <el-form-item label="是否必填" prop="questionnaireTitle">
            <el-radio v-model="addQuestionForm.required" label="1">是</el-radio>
            <el-radio v-model="addQuestionForm.required" label="0">否</el-radio>
          </el-form-item>
        </el-form>
      </div>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addQuestionVisible = false">取 消</el-button>
        <el-button type="primary" @click="addQuestion">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 获取问卷列表的参数对象
      querryInfo: {
        // 当前页码
        current: 1,
        // 每页显示记录数
        size: 2
      },
      total: 0,
      questionnaireList: [],
      // 添加问卷对话框显示与隐藏
      addDialogVisible: false,
      // 添加问卷对话框表单数据
      addQuestionnaireForm: {
        questionnaireTitle: '',
        questionnaireExplain: '',
        // startTime: '',
        // endTime: '',
        creatorAccount: ''
      },
      // 添加问卷对话框表单验证规则
      addQuestionnaireRules: {
        questionnaireTitle: [],
        questionnaireExplain: [],
        // startTime: [],
        // endTime: [],
        creatorAccount: []
      },
      // 添加问题对话框
      addQuestionVisible: false,
      addQuestionForm: {
        questionnaireId: '',
        options: [{
          content: ''
        },
        {
          content: ''
        },
        {
          content: ''
        },
        {
          content: ''
        }
        ],
        questionTitle: '',
        type: '1',
        required: '1'
      },
      addQuestionRules: {

      },
      // 整张问卷
      quetionnaire: {}
    }
  },
  created () {
    this.getQuestionnaireList()
  },
  methods: {
    // 获取问卷列表
    async getQuestionnaireList () {
      const { data: res } = await this.$http.get('/questionnaire-do/list', {
        params: this.querryInfo
      })
      // console.log(res)
      if (!res.success) return this.$message.error('获问卷列表失败')
      this.questionnaireList = res.data
      this.total = res.total
    },
    // 监听 size 改变的事件
    handleSizeChange (newSize) {
      // 改变后重新赋值，然后重新发起请求
      this.querryInfo.size = newSize
      this.getQuestionnaireList()
    },
    // 监听current 改变的事件
    handleCurrentChange (newCurrent) {
      this.querryInfo.current = newCurrent
      this.getQuestionnaireList()
    },
    // 监听删除按钮点击事件
    async deleteQuestionnaire (id) {
      console.log(id)
      const confirmResult = await this.$confirm('此操作将永久删除该问卷, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).catch(err => err)
      // 用户点击取消
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      // 点击确定
      console.log('确定')
      // 删问卷
      const { data: res } = await this.$http.delete('/questionnaire-do/delete/' + id)
      // 删问题
      this.$http.delete('/question-do/deleteByQuestionnaireId/' + id)
      // 删选项
      this.$http.delete('/options-do/deleteByQuestionnaireId/' + id)
      if (!res.success) this.$message.error('删除失败')
      this.$message.success('删除成功')
      this.getQuestionnaireList()
    },
    // 监听添加问卷对话框关闭事件，清除数据
    addDialogClosed () {
      this.$refs.addQuestionnaireRef.resetFields()
    },
    // 监听添加问卷对话框确定按钮点击事件，添加问卷
    addQuestionnaire () {
      this.$refs.addQuestionnaireRef.validate(async valid => {
        if (!valid) return
        // 用户表单预校验通过，可以发起网络请求添加用户
        const { data: res } = await this.$http.post('/questionnaire-do/add', this.addQuestionnaireForm)
        console.log(res)
        if (!res.success) this.$message.error('添加问卷失败')
        this.$message.success('添加问卷成功')
        // 隐藏添加问卷对话框
        this.addDialogVisible = false
        // 重新获取问卷列表
        this.getQuestionnaireList()
      })
    },
    // 监听添加问题点击事件
    showAddQuestionDialog (questionnaireId) {
      this.addQuestionForm.questionnaireId = questionnaireId
      this.addQuestionVisible = true
    },
    // 监听添加问题对话框关闭事件清除数据，为啥这里失效？？
    addQuestionDialogClosed () {
      this.$refs.addQuestionRef.resetFields()
    },
    // 添加问题
    addQuestion () {
      this.$refs.addQuestionRef.validate(async valid => {
        if (!valid) return
        // 用户表单预校验通过，可以发起网络请求添加用户
        const { data: res } = await this.$http.post('/question-do/add', this.addQuestionForm)
        if (!res.success) this.$message.error('添加问题失败')
        this.$message.success('添加问题成功')
        // 获取问题id
        var questionId = res.data.id
        // 添加选项 如果是选择题的话
        if (this.addQuestionForm.type === '1' || this.addQuestionForm.type === '2') {
          for (let i = 0; i < this.addQuestionForm.options.length; i++) {
            this.addQuestionForm.options[i].questionnaireId = this.addQuestionForm.questionnaireId
            this.addQuestionForm.options[i].type = this.addQuestionForm.type
            this.addQuestionForm.options[i].questionId = questionId
            this.$http.post('/options-do/add', this.addQuestionForm.options[i])
          }
        }
        // 隐藏添加问卷对话框
        this.addQuestionVisible = false
      })
    },
    // 填写问卷 封装成整张问卷对象
    async doQuestionnaire (questionnaireId) {
      this.$router.push({
        name: 'Dequetionnaire',
        params: { questionnaireId: questionnaireId }
      })
    },
    // 问卷统计
    async statisticQuestionnaire (questionnaireId) {
      this.$router.push({
        name: 'Statistics',
        params: { questionnaireId: questionnaireId }
      })
    }
  }

}
</script>

<style lang="less" scoped>
</style>
